package com.tim.android.utils;

import java.util.Locale;
import java.util.Random;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

public class ComputeTool {

	private static final String TAG = "ComputeTool";
	private static int SCREEN_SIZE_1 = -1;
	private static int SCREEN_SIZE_2 = -1;
	
	public static int computePercentOfScreenWidth(Context context, int percent) {
		return getScreenWidth(context) * percent / 100;
	}

	public static int computePercentOfScreenHeight(Context context, int percent) {
		return getScreenHeight(context) * percent / 100;
	}

	public static int getScreenWidth(Context context) {
		if(SCREEN_SIZE_1 <= 0 || SCREEN_SIZE_2 <=0 ){
			DisplayMetrics dm = new DisplayMetrics();
			WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
			wm.getDefaultDisplay().getMetrics(dm);
			SCREEN_SIZE_1 = dm.widthPixels;
			SCREEN_SIZE_2 = dm.heightPixels;
		}
		
		Configuration conf = context.getResources().getConfiguration();
		switch(conf.orientation){
		case Configuration.ORIENTATION_LANDSCAPE:
			return SCREEN_SIZE_1 > SCREEN_SIZE_2 ? SCREEN_SIZE_1:SCREEN_SIZE_2;
		case Configuration.ORIENTATION_PORTRAIT:
			return SCREEN_SIZE_1 < SCREEN_SIZE_2 ? SCREEN_SIZE_1:SCREEN_SIZE_2;
		default:
				Log.e(TAG, "can't get screen width!");
		}
		return SCREEN_SIZE_1;
	}

	public static int getScreenHeight(Context context) {
		if(SCREEN_SIZE_1 <= 0 || SCREEN_SIZE_2 <=0 ){
			DisplayMetrics dm = new DisplayMetrics();
			WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
			wm.getDefaultDisplay().getMetrics(dm);
			SCREEN_SIZE_1 = dm.widthPixels;
			SCREEN_SIZE_2 = dm.heightPixels;
		}

		Configuration conf = context.getResources().getConfiguration();
		switch(conf.orientation){
		case Configuration.ORIENTATION_LANDSCAPE:
			return SCREEN_SIZE_1 < SCREEN_SIZE_2 ? SCREEN_SIZE_1:SCREEN_SIZE_2;
		case Configuration.ORIENTATION_PORTRAIT:
			return SCREEN_SIZE_1 > SCREEN_SIZE_2 ? SCREEN_SIZE_1:SCREEN_SIZE_2;
		default:
				Log.e(TAG, "can't get screen height!");
		}
		return SCREEN_SIZE_1;
	}

	/**
	 * 判断是否为平板
	 * 
	 * @return
	 */
	public static boolean isPad(Context context) {
		if (android.os.Build.VERSION.SDK_INT == 11		||
				android.os.Build.VERSION.SDK_INT == 12	||
				android.os.Build.VERSION.SDK_INT == 13) {
			return true;
		}
		DisplayMetrics dm = new DisplayMetrics();
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		wm.getDefaultDisplay().getMetrics(dm);
		// Log.d(TAG, "isPad? dm="+dm+" densityDpi:"+dm.densityDpi+" ");
		if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
			return dm.widthPixels >= 768 && dm.heightPixels >= 1024;
		} else if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			return dm.widthPixels >= 1024 && dm.heightPixels >= 768;
		}
		return false;
	}

	public static boolean isSmallPhone(Context context) {
		DisplayMetrics dm = new DisplayMetrics();
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		wm.getDefaultDisplay().getMetrics(dm);
		if (dm.densityDpi <= 160 && android.os.Build.VERSION.SDK_INT <= 10) {
			return true;
		}
		return false;
	}

	public static int getHeightByText(String text, int width) {
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setTextSize(20);
		float totalWidth = paint.measureText(text);
		float totalRowNumber = totalWidth / width + (totalWidth % width == 0 ? 1 : 0);
		FontMetrics fm = paint.getFontMetrics();
		float fontHeight = fm.bottom - fm.top;
		return (int) ((totalRowNumber + 2) * fontHeight);
	}
	
	public static boolean isMediumDensity(Context context) {
		DisplayMetrics dm = new DisplayMetrics();
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		wm.getDefaultDisplay().getMetrics(dm);
		if (dm.densityDpi <= 160) {
			return true;
		}
		return false;
	}
	
	/**
	 * dip单位值转成px单位值
	 * @param px单位的值
	 * @return dip单位的值
	 */
	public static float px2dip(Context context, int px) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (px - 0.5f) / scale;
	}
	
	/**
	 * dip单位值转成px单位值
	 * @param dip dip单位的值
	 * @return px单位的值
	 */
	public static float dip2px(Context context, int dip) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (dip * scale + 0.5f);
	}
	
	/**
	 * Get Android System version, eg: 2.3
	 * @param context
	 * @return version
	 * @author Tim
	 */
	public static String getOSVersion(Context context) {
		return android.os.Build.VERSION.RELEASE;
	}
	
	/**
	 * Get screen density.
	 * @param context
	 * @return density
	 * @author Tim
	 */
	public static float getDensity(Context context) {
		return context.getResources().getDisplayMetrics().density;
	}
	
	/**
	 * Get screen density dpi.
	 * @param context
	 * @return density dpi
	 * @author Tim
	 */
	public static int getDensityDpi(Context context) {
		return context.getResources().getDisplayMetrics().densityDpi;
	}
	
	/**
	 * Get current screen orientation, one of {@link #ORIENTATION_LANDSCAPE}, {@link #ORIENTATION_PORTRAIT},
     * or {@link #ORIENTATION_SQUARE} in Class android.content.res.Configuration.
	 * @param context
	 * @return orientation
	 * @author Tim
	 */
	public static int getOrientation(Context context) {
		return context.getResources().getConfiguration().orientation;
	}
	
	/**
	 * Generate random number, exclude zero.
	 * @param howMany how many number you want generate.
	 * @return Random number string.
	 * @author Tim
	 */
	public static String generateRandomNumber(int howMany) {
		String result = "";
		Random random = new Random();
		for(int i = 0; i < howMany; i++) {
			int randomNumber = random.nextInt(8) + 1;
			result += randomNumber;
		}
		return result;
	}
	
	/**
	 * Get current system locale
	 * @param context
	 * @return locale
	 * @author Tim
	 */
	public static Locale getSystemLocale(Context context) {
		return context.getResources().getConfiguration().locale;
	}
	
	/**
	 * Get the application version which is launching 
	 * @param context
	 * @return application version
	 * @author Tim
	 */
	public static String getAppVersion(Context context) {
		String version = null;
		try {
			PackageManager pm = context.getPackageManager();
			PackageInfo info = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_CONFIGURATIONS);
			version = info.versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return version;
	}
	
	/**
	 * Get the running activities in this application (need "android.permission.GET_TASKS" permission).
	 * @param context
	 * @return size of running activities.
	 */
	public static int countRunningActivities(Context context) {
		ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		RunningTaskInfo task = null;
		try {
			task = activityManager.getRunningTasks(1).get(0);
		} catch (SecurityException e) {
			Log.e(TAG, "No \"android.permission.GET_TASKS\" permission.");
			task = null;
		}
		return task == null ? 0 : task.numRunning;
	}
}
